package org.study.redisson.boot.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.study.redisson.boot.util.RedissonLockUtils;

import java.io.IOException;

/**
 * 通过 YAML 格式文件配置 Redisson
 *
 * @author Administrator
 * @date 2021-06-02
 */
//@Configuration
public class RedissonFileConfig {
    @Bean
    public RedissonClient redissonClient() throws IOException {
        // 单机模式
        Config single = Config.fromYAML(new ClassPathResource("redisson-config-file-single.yml").getInputStream());
        //Config single = Config.fromYAML(new File(RedissonFileConfig.class.getClassLoader().getResource("redisson-config-file-single.yml").getPath()));

        // 主从模式
        //Config masterslave = Config.fromYAML(new ClassPathResource("redisson-config-file-masterslave.yml").getInputStream());
        //Config masterslave = Config.fromYAML(new File(RedissonFileConfig.class.getClassLoader().getResource("redisson-config-file-masterslave.yml").getPath()));

        // 哨兵模式
        //Config sentinel = Config.fromYAML(new ClassPathResource("redisson-config-file-sentinel.yml").getInputStream());
        //Config sentinel = Config.fromYAML(new File(RedissonFileConfig.class.getClassLoader().getResource("redisson-config-file-sentinel.yml").getPath()));

        // 集群模式
        //Config cluster = Config.fromYAML(new ClassPathResource("redisson-config-file-cluster.yml").getInputStream());
        //Config cluster = Config.fromYAML(new File(RedissonFileConfig.class.getClassLoader().getResource("redisson-config-file-cluster.yml").getPath()));

        // 云托管模式
        //Config replicated = Config.fromYAML(new ClassPathResource("redisson-config-file-replicated.yml").getInputStream());
        //Config replicated = Config.fromYAML(new File(RedissonFileConfig.class.getClassLoader().getResource("redisson-config-file-replicated.yml").getPath()));

        RedissonClient redissonClient = Redisson.create(single);
        // 给工具类注入它需要的Bean
        RedissonLockUtils.setRedissonClient(redissonClient);
        return redissonClient;
    }
}
